

import {
	HTTP_REQUEST_URL,
	TOKEN_KEY,
	DEBUGGER
} from "../config";
import _ from 'lodash'


// #ifdef H5
import {
	browser
} from "./browser";
// #endif


// import store from "../store";

export default (options) => {
	const success = "color: #fff;background: #2ea822";
	const fail = "color: #fff;background: red";
	let baseUrl = "";
	// #ifdef H5
	// h5本地环境走代理， 解决跨域
	if (process.env.NODE_ENV === "development") {
		baseUrl = "/api";
	}
	// #endif

	// #ifdef MP-WEIXIN
	baseUrl = HTTP_REQUEST_URL;
	// #endif
	let headers = {};
	headers["Content-Type"] =
		options.contentType || "application/x-www-form-urlencoded";

	//token认证
	let TOKEN = uni.getStorageSync("token");
	console.log('options', {
		url: (options.externalRequestUrl ? options.externalRequestUrl : baseUrl) +
			options.url,
		method: options.method || "POST",
		data: options.params,
		header: headers,
	})
	if (TOKEN) {
		headers[TOKEN_KEY] = TOKEN;
	}

	return new Promise((resolve, reject) => {
		uni.request({
			url: (options.externalRequestUrl ? options.externalRequestUrl : baseUrl) +
				options.url,
			method: options.method || "POST",
			data: options.params,
			header: headers,
			success(res) {
				if (typeof res !== "object") {
					uni.showToast({
						icon: "none",
						title: "系统错误请联系管理员",
						duration: 3000,
					});
					reject(res);
					return;
				} else {
					// if (res.data.code !== 1) return reject(res.data)
					if (res.data.code === 1 || res.data.statusCode === 200) {
						if(DEBUGGER){
							console.log(
								"%c☀---------------------------[ start ]-----------------------------",
								success
							);
							console.log(`    请求方式：${options.method}`);
							console.log(`    请求时间：${new Date()}`);
							console.log(`    请求URL：[ ${options.url} ]`);
							console.log(
								`    请求参数：`,
								options.params === undefined ? "不需要请求参数" : JSON.stringify(options
									.params)
							);
							console.log("    返回结果：", res);
							console.log(
								"%c☀---------------------------[  end  ]-----------------------------",
								success
							);
						}

						resolve(res.data.data);
					} else {
						uni.showToast({
							icon: "none",
							title: res.data.msg,
							duration: 2000,
						});
						if(DEBUGGER){
							console.log(
								"%c☁---------------------------[ start ]-----------------------------",
								fail
							);
							console.error(`    请求方式：${options.method}`);
							console.error(`    请求时间：${new Date()}`);
							console.error(`    请求URL：[ ${options.url} ]`);
							console.error(
								`    请求参数：`,
								options.params === undefined ? "不需要请求参数" : options.params
							);
							console.error("    返回结果", res.data);
							console.log(
								"%c☁---------------------------[ end ]-----------------------------",
								fail
							);
						}

						if (res.data.rspCode === 10401) {
							console.log('res.data.rspCode === 10401')
							if (uni.getStorageSync('toLogin')) {
								return;
							}

							// store.commit('user/CLEAR_USER_DATA', {})
							// store.commit('tim/INIT_STATE')
							uni.clearStorage()
							uni.setStorageSync('toLogin', true)

							// #ifdef H5
							if (browser.versions.weiXin) {
								// wxJsSdkConfig({
								// 	url: location.href.split('#')[0]
								// }).then(sdkConfig => {
								// 	location.href =
								// 		`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${sdkConfig.appId}&redirect_uri=${location.href}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
								// 	//
								//
								// })
							} else {
								toPage('/C/login/login?back=1')
							}
							// #endif

						}
						if (res.data.rspCode === 10402) {
							this.isLogoutShow = false
							const _this = this
							// store.commit('user/CLEAR_USER_DATA', {})
							// store.commit('tim/INIT_STATE')
							uni.clearStorage()

							this.isLogoutShow = false
							uni.showModal({
								title: '提示',
								content: '您的账号已在其他地方登录， 请检查密码是否泄露',
								success: function(res) {
									if (res.confirm) {
										console.log('用户点击确定');
										toPage('/C/login/login?back=1')
									} else if (res.cancel) {
										console.log('用户点击取消');
									}
								}
							});
						}

						reject(res.data);
					}
				}
			},
			fail(err) {
				uni.hideLoading()

				reject(err);
			},
		});
	})
};




function toPage(url) {
	uni.navigateTo({
		url
	})
}

